package net.sdd.ucc.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sdd.common.result.Result;
import net.sdd.ucc.dto.LoginDto;
import net.sdd.ucc.dto.RegisterDto;
import net.sdd.ucc.entity.Member;
import net.sdd.ucc.service.IMemberService;
import net.sdd.ucc.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2022-08-26
 */
@RestController
@RequestMapping("/ucc/member")
@Api(tags = "会员管理")
@CrossOrigin
public class MemberController {
    @Autowired
    private IMemberService memberService;

    /**
     * 注册实现
     * @param registerDto dto用来封装数据传输对象的。Data Transfer Object
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody RegisterDto registerDto){
        memberService.register(registerDto);
        return Result.ok();
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
        String token = memberService.login(loginDto);
        return Result.ok().data("token",token);
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("findMemberByToken")
    public Result findMemberByToken(String token){
        String memberId = JwtUtils.getMemberIdByJwtToken(token);
        Member member = memberService.getById(memberId);
        return Result.ok().data("member",member);
    }
}






















